package cn.lena.idle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.lena.idle.dao.UserDbHelper;
import cn.lena.idle.pojo.DTO.Version;

public class ChangePasswordActivity extends AppCompatActivity {

    private Button change;
    private ImageView returnbefore2;
    private String newPass;
    private String rePass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        // 修改密码 t
        change=(Button)findViewById(R.id.change_pass_btn);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePass();
            }
        });

        // 返回按钮 t
        returnbefore2=(ImageView)findViewById(R.id.returnbefore2);
        returnbefore2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ChangePasswordActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 修改密码 t
     */
    public void changePass(){
        newPass=((EditText)findViewById(R.id.new_pass)).getText().toString().trim();
        rePass=((EditText)findViewById(R.id.repeat_pass)).getText().toString().trim();
        if (newPass.equals("")||rePass.equals("")) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if (!newPass.equals(rePass)) {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
            return;
        }
        UserDbHelper user_db=new UserDbHelper(getApplicationContext(),UserDbHelper.DB_NAME,null, Version.USER);
        if(user_db.updatePassword(newPass) == 1) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(ChangePasswordActivity.this,MyActivity.class);
            startActivity(intent);
        }else{
            Toast.makeText(this, "修改失败", Toast.LENGTH_LONG).show();
            return;
        }
    }
}
